﻿using Sunny.UI;
using System;
using CentralVacuum.Common;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace CentralVacuum.Froms
{
    public partial class FLicense : UIForm
    {
        public IniFile ini;
        private string original = "";
        public FLicense()
        {
            InitializeComponent();
            ini = new IniFile(@"C:\FangYuanData\license.lic");
        }

        private void FLicense_Load(object sender, EventArgs e)
        {
            original = Global.GetCpuInfo();//授权码
            uiTextBox1.Text = original;
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            // Create a new instance of the Aes
            // class.  This generates a new key and initialization
            // vector (IV).
            using (Aes myAes = Aes.Create())
            {
                // Encrypt the string to an array of bytes.
                byte[] encrypted = Global.EncryptStringToBytes_Aes(uiTextBox1.Text, myAes.Key, myAes.IV);
                string lic = @"C:\FangYuanData\license.lic";
                IniFile ini = new IniFile(lic);
                ini.Write("License", "Encrypted", Convert.ToBase64String(encrypted));//
                ini.Write("License", "Key", Convert.ToBase64String(myAes.Key));//
                ini.Write("License", "IV", Convert.ToBase64String(myAes.IV));//
                ini.UpdateFile();
                Console.WriteLine("Original:   {0}", uiTextBox1.Text);

                UIMessageTip.ShowOk("Success!");

                Global.LogNet.WriteInfo(this.Name.ToString(), "授权成功!PN:" + original);
                /*string vKey = Convert.ToBase64String(myAes.Key);
                string vIV = Convert.ToBase64String(myAes.IV);



                string strEncrypted = ini.ReadString("License", "Encrypted", "");
                string strKey = ini.ReadString("License", "Key", "");
                string strIV = ini.ReadString("License", "IV", "");
                // Decrypt the bytes to a string.
                string roundtrip = Global.DecryptStringFromBytes_Aes(Convert.FromBase64String(strEncrypted), Convert.FromBase64String(strKey), Convert.FromBase64String(strIV));

                //Display the original data and the decrypted data.
                
                Console.WriteLine("Round Trip: {0}", roundtrip);*/
            }
            Global.IsLogin = 0;
            Close();
        }
    }
}
